package ar.com.quickmart.mainmenu;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import ar.com.quickmart.R;
import ar.com.quickmart.app.MainActivity;
import ar.com.quickmart.configuration.ConfigurationManager;
import ar.com.quickmart.mainmenu.compareprices.ComparePricesActivity;
import ar.com.quickmart.mainmenu.linktosupermarket.LinkToSupermarketActivity;
import ar.com.quickmart.mainmenu.lists.ListsActivity;
import ar.com.quickmart.mainmenu.purchasehistory.PurchaseHistoryActivity;
import ar.com.quickmart.mainmenu.purchaselist.PurchaseListActivity;
import ar.com.quickmart.mainmenu.statistics.StatisticsActivity;
import ar.com.quickmart.mainmenu.statistics.SpentInMonthResultActivity;

public class MainMenu extends Activity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_menu);

		String mUsername = "";
		
		// Verifico si hay alguien logueado, sino mando a pantalla de login/registro
		if(ConfigurationManager.getInstance(MainMenu.this).isUserLogged()) {
			mUsername = ConfigurationManager.getInstance(MainMenu.this).getUserName();
		}
		else {
			Intent intent = new Intent(MainMenu.this, MainActivity.class);
			startActivity(intent);
			finish();
		}
		
		// Obtengo elementos de UI
		TextView UITxtUsername = (TextView) findViewById(R.id.txtUserActual);
		
		// Recibo el nombre de usuario desde el intent de login
		//Bundle data = getIntent().getExtras();
		//String username = (String) data.get("username");
		
		// Modifico el username en la interfaz
		UITxtUsername.setText(mUsername);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main_menu, menu);
		return true;
	}
	
	public void logout(View view) {
		
		// Indico que deslogueo
		ConfigurationManager.getInstance(MainMenu.this).logout();
		
		// Inicio MainActivity
		Intent intent = new Intent(MainMenu.this, MainActivity.class);
		startActivity(intent);
		
		// Finalizo
		finish();		
	}
	
	public void lists(View view) {
		// Creo el intent para abrir el panel de listas
		Intent intent = new Intent(this, ListsActivity.class);
		startActivity(intent);
	}
	
	public void linkToSupermarket(View view) {
		// Creo el intent para ver las listas asociar
		Intent intent = new Intent(this, LinkToSupermarketActivity.class);
		startActivity(intent);
	}
	
	public void purchaseHistory(View view) {
		// Inicio el registro y espero el resultado, para saber si fue satisfactorio
		Intent intent = new Intent(this, PurchaseHistoryActivity.class);		
		startActivityForResult(intent, 1);
	}
	
	public void purchaseList(View view) {
		Intent intent = new Intent(this, PurchaseListActivity.class);
		startActivity(intent);
	}
	
	public void comparePrices(View view) {
		Intent intent = new Intent(MainMenu.this, ComparePricesActivity.class);
		startActivity(intent);
	}
	
	public void showStatistics(View view) {
		Intent intent = new Intent(MainMenu.this, StatisticsActivity.class);
		startActivity(intent);
	}
}
